package com.example.app.components.MyTip

import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Cancel
import androidx.compose.material.icons.rounded.CheckCircle
import androidx.compose.material.icons.rounded.Info
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import com.example.app.cls.MyState
import com.example.app.components.MyIcon.MyIcon
import com.example.app.components.MyListItem.MyListItem
import com.example.app.components.MyText.MyText
import com.example.app.ui.theme.shapeMedium
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

enum class MyTipType(
        var type: String,
        var icon: ImageVector? = null
) {
    SUCCESS(
        "成功",
        Icons.Rounded.CheckCircle
    ),
    FAIL(
        "失败",
        Icons.Rounded.Cancel
    ),
    INFO(
        "信息",
        Icons.Rounded.Info
    )
}

class MyTipState : MyState() {
    //标题
    var _title = MutableStateFlow<String>("")
    var title = _title.asStateFlow()
    fun flowUpdate_title(value: String) {
        _title.update {
            value
        }
    }
    //描述
    var _description = MutableStateFlow<String?>(null)
    var description = _description.asStateFlow()
    fun flowUpdate_description(value: String) {
        _description.update {
            value
        }
    }
    //类别
    var _type = MutableStateFlow<MyTipType?>(null)
    var type = _type.asStateFlow()
    fun flowUpdate_type(value: MyTipType) {
        _type.update {
            value
        }
    }
    //持续时间
    companion object {
        val MYTIP_LONG = 5000L//长时间
        val MYTIP_SHORT = 2000L//短时间
        val MYTIP_INDEFINITE = 0L//自定义时间
    }
    //持续时间
    var _duration = MutableStateFlow<Long>(MYTIP_LONG)
    var duration = _duration.asStateFlow()
    fun flowUpdate_duration(value: Long) {
        _duration.update {
            value
        }
    }

    fun show(
            type: MyTipType = MyTipType.SUCCESS,
            title: String = "提示",
            description: String = "",
            duration: Long = MyTipState.MYTIP_LONG
    ) {
        flowUpdate_type(type)
        flowUpdate_title(title)
        flowUpdate_description(description)
        flowUpdate_duration(duration)
        flowUpdate_visible(true)
    }
}
@Composable
fun MyTip(
        myTipState: MyTipState? = null
) {
    var visible = myTipState?.visible?.collectAsState()
    var type = myTipState?.type?.collectAsState()
    var description = myTipState?.description?.collectAsState()
    var title = myTipState?.title?.collectAsState()
    var duration = myTipState?.duration?.collectAsState()
    var scope = rememberCoroutineScope()


    if (visible != null && visible.value) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .offset(y = 120.dp),
            contentAlignment = Alignment.Center
        ) {
            Row(
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .clip(RoundedCornerShape(shapeMedium))
                    .background(Color.Black.copy(.65f))
            ) {
                type?.value!!.let {
                    TipContent(
                        type = it,
                        title = title?.value,
                        description = description?.value
                    )
                }
            }
        }
    }

    if (visible != null && visible.value && duration != null && duration.value != MyTipState.MYTIP_INDEFINITE) {
        scope.launch {
            delay(duration.value)
            myTipState?.flowUpdate_visible(false)
        }
    } else {
    }
}
@Composable
fun TipContent(
        type: MyTipType,
        title: String? = null,
        description: String? = null,
) {
    var windowWidth = with(LocalDensity.current) {
        LocalConfiguration.current.screenWidthDp
    } * .7f
    MyListItem(
        modifier = Modifier.width(windowWidth.dp),
        colors = ListItemDefaults.colors(
            containerColor = Color.Transparent,
        ),
        hasWrap = false,
        leadingContent = {
            MyIcon(
                imageVector = type.icon!!,
                tint = MaterialTheme.colorScheme.surface
            )
        },
        headlineContent = {
            MyText(
                text = if (title != null) title else type.type,
                color = MaterialTheme.colorScheme.surface
            )
        },
        supportingContent = {
            MyText(
                text = if (description != null) description else "",
                color = MaterialTheme.colorScheme.surface,
                style = MaterialTheme.typography.bodySmall
            )
        }
    )
}
